import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'
import http from '@/utils/uc-http'
import { connect, UserInfo } from '../api/user'

const TOKEN_KEY = 'token'

export const useUserStore = defineStore('user', () => {
  const route = useRoute()
  const router = useRouter()
  const packageJs = ref({})
  const showerrordialog = ref(false)
  // const userdata = ref({})
  const token = ref(localStorage[TOKEN_KEY])
  // const expires = ref(localStorage.expires)
  const userInfo = ref(JSON.parse(localStorage.userInfo || '{}'))
  

  // 判断是否登录
  // const checkLogin = (isGoLogin = true) => {
  //   token.value = localStorage[TOKEN_KEY]

  //   if (token.value) {
  //     // isGoLogin = false
  //     return true
  //   } else {
  //     // if (isGoLogin) logout(true)
  //     return false
  //   }
  // }
  const checkLogin = async (isGoLogin = true) => {
    token.value = localStorage[TOKEN_KEY]
    if (token.value) {
      // setTimeout(() => {
      //   checkInfo()
      // }, 10000);
      return true
    } else {
      if (isGoLogin){
        ElMessage.warning('Token过期或无效,请重新登录')
        logout(true)
      } 
      return false
    }
  }
  // 登录成功
  const loginSuccess = async options => {
    console.log(options);
    
    token.value = options.accessToken
    localStorage.userInfo = JSON.stringify(options)
    localStorage.uid = options.uid
    localStorage.plant_code = options.plant_code
    localStorage[TOKEN_KEY] = token.value
    localStorage.roles = JSON.stringify(options.roles)
    const body ={
        platform: 'web',
        registration_id: 0,
        type: 'on',
        uid: localStorage.uid
      }
      connect(body).then(res => {
        localStorage.hasConnect = res
      })
      ElMessage.success('登录成功');
    // 刷新页面并跳转到 "/home"
    // setTimeout(() => {
    //   window.location.replace('');  // 刷新页面并跳转到首页
    // }, 1000);
    router.replace({
      path: '/match/live',
    })
  }

  const registerSuccess = async options => {
    ElMessage.success('注册成功');
    router.replace({
      path: '/login'
    })
  }

  // 判断账号是否有效
  const checkInfo = async () => {

    if (!token.value) return
    let mac = await window.electron.getMacAddress();


    if (localStorage.plant_code) {
      // console.log(token.value);

      http.post('/user_check', {
        token: token.value,
        plant_code: localStorage.plant_code,
        // mac: mac,
        mac: localStorage.mac,
      }).then((res) => {
        // userdata.value = res
        // jurisdiction(res)
      })
    }
  }
  checkInfo()
  setInterval(checkInfo, 30 * 1000)

  // 退出
  const logout = () => {
    localStorage.removeItem('token')
    localStorage.removeItem('mac')
    localStorage.removeItem('plant_code')
    localStorage.removeItem('userInfo')
    ElMessage.success('退出成功');
    userInfo.value = null
    token.value = null
    router.replace({
      path: '/login',
    })
  }
  const savepackageJs = (packagejs) => {
    packageJs.value = packagejs
  }
  const openerrordialog = () => {
    showerrordialog.value = true
  }

  return {
    userInfo,
    showerrordialog,
    // roles,
    registerSuccess,
    openerrordialog,
    checkLogin,
    loginSuccess,
    savepackageJs,
    // getUserInfo,
    logout,
  }
})
